---
title: "Voter ID Reproduction Document"
author: "Thomas Altmann"
date: "3/5/2021"
output:
  pdf_document: default
  html_document: default
---

This is the reproduction file for the paper entitled: "Signaling Minorities: A Novel Theoretical Proposition for Voter Identification Laws and Minority Suppression" by Thomas Altmann. 

The document contains the R code used to produce each model and figure along with the output of the code.

The following packages are used: dplyr, ggplot2, haven, MASS, stargazer, effects, ordinal, ggeffects, fastDummies.
```{r include=FALSE}
library(dplyr)
library(ggplot2)
library(haven)
library(MASS)
library(stargazer)
library(effects)
library(ordinal)
library(ggeffects)
library(fastDummies)
library(lmtest)
library(sandwich)
library(robust)

spae_2014 <- read_dta("~/Desktop/Voter_ID_Paper/R/SPAE_2014.dta")
spae_2014 <- dummy_cols(spae_2014, select_columns = "pid3")

cbPalette <- c("#999999", "#E69F00", "#56B4e9", "#009E73", "#F0E442", 
  "#0072B2", "#D55E00", "#CC79A7")
```


```{r include=FALSE}
str(spae_2014)
```


# Describing the Data

## Summary Statisitics Table
```{r}
t1 <- data.frame(spae_2014$Q33, spae_2014$Q35, spae_2014$asked_ID, 
  spae_2014$state_ID_law, spae_2014$pid3_1, spae_2014$race)
```

```{r echo=FALSE, results='asis'}
stargazer(t1, type = "latex", median = TRUE)
```



```{r echo = T, results = 'hide'}
spae_2014$Q33 <- ordered(spae_2014$Q33, levels = 1:4, 
  labels = c("Very confident","Somewhat confident",
    "Not too confident","Not at all confident"))
spae_2014$Q35 <- ordered(spae_2014$Q35, levels = 1:4, 
  labels = c("Very confident","Somewhat confident",
    "Not too confident","Not at all confident"))

```

## Plotting the Data (first dependent variable)
```{r}
spae_2014 %>% mutate(Q33 = ordered(Q33, levels=rev(levels(Q33))), 
  asked_ID = as.factor(asked_ID)) %>% ggplot(aes(x=asked_ID, fill=Q33)) + 
  geom_bar(asked_ID = "fill") + scale_fill_manual(values=cbPalette) + 
  theme_minimal() + theme(text=element_text(size=10, family = "Times")) + 
  labs(title="Data Plotted by Asked ID and Confidence in General Vote") + 
  ylab("Count") + xlab("Asked ID")
```

## Plotting the Data (second dependent variable)
```{r}
spae_2014 %>% mutate(Q35 = ordered(Q35, levels=rev(levels(Q35))), 
  asked_ID = as.factor(asked_ID)) %>% 
  ggplot(aes(x=asked_ID, fill=Q35)) + geom_bar(asked_ID = "fill") + 
  scale_fill_manual(values=cbPalette) + theme_minimal() + 
  theme(text=element_text(size=10, family = "Times")) + 
  labs(title="Data Plotted by Asked ID and Confidence in State Vote") + 
  ylab("Count") + xlab("Asked ID")
```

# First Main Independent Variable

## Model 1
Confidence in general election vote interacted on being asked for ID. Ordered logistic regression weighted by national representation.

```{r}
fit1 <- clm(Q33 ~ asked_ID, data=spae_2014,  weights = weight, family = binomial(link="logit"))

fit1a <- glm(Q33 ~ asked_ID + race, data=spae_2014,  weights = weight, family = "binomial")
summary(fit1a)

newdat <- data.frame(asked_ID = c(0,1))
fit1predict <- cbind(newdat, predict(fit1, newdat, type="prob")$fit)

ggpredictions_fit1 <- ggpredict(fit1, terms=c("asked_ID"))
ggpredictions_fit1$x <- factor(ggpredictions_fit1$x)
levels(ggpredictions_fit1$x) = c("Not Asked ID","Asked ID")
colnames(ggpredictions_fit1)[c(1,5)]=c("Asked_ID", "Q33")
```

### Plotting predictions from Model 1
```{r}
colnames(ggpredictions_fit1) <- make.unique(names(ggpredictions_fit1))
ggplot(ggpredictions_fit1, aes(x=Q33, y=predicted)) + 
  geom_point(aes(color=Asked_ID), asked_ID=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = Asked_ID), 
  asked_ID=position_dodge(width = 0.5), width=0.3) + 
  theme_minimal() + scale_color_manual(values=cbPalette[2:3]) + 
  labs(title="Predicted Probabilities for Confidence in Vote when Asked for ID",
  color="Asked ID") + ylab("Predicted Probabilities") + 
  xlab("Confidence in General Election Vote") + 
  theme(text=element_text(size=10, family = "Times"))
```

## Model 2 
Confidence in general election vote interacted on being asked for ID and Democrat dummy variable. Ordered logistic regression weighted by national representation.
```{r}
fit2 <- clm(Q33~asked_ID + pid3_1, weights=weight, data=spae_2014, link="logit")

ggpredictions_fit2 <- data.frame(ggpredict(fit2, terms=c("asked_ID","pid3_1")))
ggpredictions_fit2$x <- factor(ggpredictions_fit2$x)
levels(ggpredictions_fit2$x) = c("Not Asked ID","Asked ID")
colnames(ggpredictions_fit2)[c(1,5,6)]=c("Asked_ID", "Q33","pid3_1")
```

### Plotting predictions from Model 2
```{r}
ggplot(ggpredictions_fit2, aes(x=Q33, y=predicted)) + 
  geom_point(aes(color=Asked_ID), asked_ID=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = Asked_ID), 
  asked_ID=position_dodge(width = 0.5), width=0.3) + theme_minimal() + 
  facet_wrap(~pid3_1) + scale_color_manual(values=cbPalette[2:3]) + 
  labs(title="Predicted Probabilities for Confidence in Vote when Asked for ID",
  color="Asked ID") + ylab("Predicted Probabilities") + 
  xlab("Confidence in General Election Vote by Democrat Dummy Variable") + 
  theme(text=element_text(size=10, family = "Times"))
```

## Model 3 
Confidence in general election vote interacted on being asked for ID and interaction term of race. Ordered logistic regression weighted by national representation.
```{r}
fit3 <- clm(Q33 ~  asked_ID * race, weights=weight, data = spae_2014, link="logit")
summary(fit3)

ggpredictions_fit3 <- data.frame(ggpredict(fit3, terms=c("asked_ID","race")))
ggpredictions_fit3$x <- factor(ggpredictions_fit3$x)
levels(ggpredictions_fit3$x) = c("Not Asked ID","Asked ID")
colnames(ggpredictions_fit3)[c(1,5,6)]=c("Asked_ID", "Q33","race")
```

### Plotting predictions from Model 3
```{r}
ggplot(ggpredictions_fit3, aes(x=Q33, y=predicted)) + 
  geom_point(aes(color=Asked_ID), asked_ID=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = Asked_ID), 
  asked_ID=position_dodge(width = 0.5), width=0.3) + 
  theme_minimal() + facet_wrap(~race) + 
  scale_color_manual(values=cbPalette[2:3]) + 
  labs(title="Predicted Probabilities for Confidence in Vote when Asked for ID") + 
  ylab("Predicted Probabilities") + 
  xlab("Confidence in General Election Vote by Race") + 
  theme(text=element_text(size=10, family = "Times"))
```

# Second Main Independent Variable

## Model 4 
Confidence in state election vote interacted on state ID law. Ordered logistic regression weighted by national representation.
```{r}
fit4 <- clm(Q35 ~ state_ID_law, weights=weight, 
  data = spae_2014, link="logit")

ggpredictions_fit4 <- data.frame(ggpredict(fit4, terms=c("state_ID_law")))
ggpredictions_fit4$x <- factor(ggpredictions_fit4$x)
levels(ggpredictions_fit4$x) = c("No requirement","non-strict and non-photo",
  "non-strict photo ID","non-photo ID","strict photo-ID")
colnames(ggpredictions_fit4)[c(1,5)]=c("state_ID_law", "Q35") 
```

### Ploting predictions from Model 4
```{r}
ggplot(ggpredictions_fit4, aes(x=Q35, y=predicted)) + 
  geom_point(aes(color=state_ID_law), state_ID_law=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = state_ID_law), 
  state_ID_law=position_dodge(width = 0.5), width=0.3) + theme_minimal() + 
  scale_fill_manual(values=cbPalette) + 
  labs(title="Predicted Probabilities for Confidence in Vote by State ID Law",
  color="State ID Law") + ylab("Predicted Probabilities") + 
  xlab("Confidence in State Election Vote") + 
  theme(text=element_text(size=10, family = "Times"))
```

## Model 5 
Confidence in state election vote interacted on state ID laws and Democrat dummy variable. Ordered logistic regression weighted by national representation.
```{r}
fit5 <- clm(Q35 ~ state_ID_law + pid3_1, 
  weights=weight, data=spae_2014, link="logit")

ggpredictions_fit5 <- data.frame(ggpredict(fit5, 
  terms=c("state_ID_law","pid3_1")))
ggpredictions_fit5$x <- factor(ggpredictions_fit5$x)
levels(ggpredictions_fit5$x) = c("No Requirement","Non-strict and Non-photo",
  "Non-strict Photo ID","Non-photo ID","Strict Photo-ID")
colnames(ggpredictions_fit5)[c(1,5,6)]=c("state_ID_law", "Q35","pid3_1")
```

### Plotting predictions from Model 5
```{r}
ggplot(ggpredictions_fit5, aes(x=Q35, y=predicted)) + 
  geom_point(aes(color=state_ID_law), state_ID_law=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = state_ID_law), 
  state_ID_law=position_dodge(width = 0.5), width=0.3) + theme_minimal() + 
  facet_wrap(~pid3_1) + scale_fill_manual(values=cbPalette) + 
  labs(title="Predicted Probabilities for Confidence in Vote by State ID Law",
  color="State ID Law") + ylab("Predicted Probabilities") + 
  xlab("Confidence in State Election Vote by Democrat Dummy Variable") + 
  theme(text=element_text(size=10, family = "Times"))
```


## Model 6
Confidence in state election vote interacted on state ID laws and interaction term of race. Ordered logistic regression weighted by national representation.
```{r}
fit6 <- clm(Q35 ~ state_ID_law * race, weights=weight, 
  data = spae_2014, link="logit")

ggpredictions_fit6 <- data.frame(ggpredict(fit6, terms=c("state_ID_law","race")))
ggpredictions_fit6$x <- factor(ggpredictions_fit6$x)
levels(ggpredictions_fit6$x) = c("No Requirement","Non-strict and Non-photo",
  "Non-strict Photo ID","Non-photo ID","Strict Photo-ID")
colnames(ggpredictions_fit6)[c(1,5,6)]=c("state_ID_law", "Q35","race")
```

### Plotting predictions from Model 6
```{r}
ggplot(ggpredictions_fit6, aes(x=Q35, y=predicted)) + 
  geom_point(aes(color=state_ID_law), state_ID_law=position_dodge(width = 0.5)) + 
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high, color = state_ID_law), 
  state_ID_law=position_dodge(width = 0.5), width=0.3) + theme_minimal() + 
  facet_wrap(~race) + scale_fill_manual(values=cbPalette) + 
  labs(title="Predicted Probabilities for Confidence in Vote by State ID Law",
  color="State ID Law") + ylab("Predicted Probabilities") + 
  xlab("Confidence in State Election Vote by Race") + 
  theme(text=element_text(size=10, family = "Times"))
```

# Regression Results Table

```{r echo=FALSE, results='asis'}
stargazer(fit1, fit2, fit3, fit4, fit5, fit6, type = "latex",
          header = FALSE,
          covariate.labels = c("Asked ID","Democrat = 1","Race","Asked ID X Race",
            "State ID Law X Race","State ID Law"),
          dep.var.labels = c("General Election Vote","State Election Vote")
          )
```
